package ipad.asia.controller;

import ipad.asia.config.NewOrder;
import ipad.asia.entity.Consumer;
import ipad.asia.entity.Express;
import ipad.asia.entity.Order;
import ipad.asia.entity.Product;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.enterprise.EnterpriseFeign;
import ipad.asia.service.express.ExpressFeign;
import ipad.asia.service.order.OrderFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.utils.MyTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class ExpressController {
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private EnterpriseFeign enterpriseFeign;
    @Autowired
    private ExpressFeign expressFeign;
    @Autowired
    private ConsumerFeign consumerFeign;

    @RequestMapping("/express/list")
    public String expressList(HttpServletRequest request, Model model){
        List<Express> expressList = expressFeign.findByShopId((Integer) request.getSession().getAttribute("shopId"));
        model.addAttribute("expressList",expressList);
        return "express";
    }


    @RequestMapping("/pay/support")
    public String paySupport(HttpServletRequest request, Model model){
        List<Express> expressList = expressFeign.findByShopId((Integer) request.getSession().getAttribute("shopId"));
        model.addAttribute("expressList",expressList);
        return "paySupport";
    }


    @RequestMapping("/express/delivery")
    public String expressDelivery(@RequestParam(value ="expressOrderId" ,required = false)String expressOrderId,
                                  @RequestParam(value ="expressConsumerId" ,required = false)String expressConsumerId,
                                  @RequestParam(value ="expressName" ,required = false)String expressName,
                                  @RequestParam(value ="expressNumber" ,required = false)String expressNumber,
                                  @RequestParam(value ="expressAddress" ,required = false)String expressAddress,
                                  HttpServletRequest request, Model model){
        if(expressNumber==null){
            model.addAttribute("message","失败：快递单号不能空");
            Order order = orderFeign.findId(Integer.parseInt(expressOrderId));
            Consumer consumer = consumerFeign.getId(order.getConsumerId());
            Product product = productFeign.getById(order.getProductId());
            NewOrder newOrder=new NewOrder();
            newOrder.setOrder(order);newOrder.setConsumer(consumer);newOrder.setProduct(product);
            model.addAttribute("newOrder",newOrder);
            return "orderDetail";
        }else {
            if (expressFeign.findByOrderId(Integer.parseInt(expressOrderId)) != null) {
                Order order = orderFeign.findId(Integer.parseInt(expressOrderId));
                Consumer consumer = consumerFeign.getId(order.getConsumerId());
                Product product = productFeign.getById(order.getProductId());
                NewOrder newOrder=new NewOrder();
                newOrder.setOrder(order);newOrder.setConsumer(consumer);newOrder.setProduct(product);
                model.addAttribute("newOrder",newOrder);
                model.addAttribute("message", "该订单已经发货！");
                return "orderDetail";
            } else {
                orderFeign.updateStatue(Integer.parseInt(expressOrderId),2);
                int shopId= (int) request.getSession().getAttribute("shopId");
              String address=  enterpriseFeign.getName((String) request.getSession().getAttribute("user")).getAddress();
                expressFeign.expressAdd(Integer.parseInt(expressOrderId), Integer.parseInt(expressConsumerId), shopId, expressName,
                        expressNumber, address, expressAddress, new Date(),1);
                return "redirect:/express/list";
            }
        }
    }
}
